package com.guang.syncronized_pro;

import com.guang.syncronized.ObjectAgeTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CountDownLatch;

/**
 * @Description 从一个例子来开始介绍
 * @Author liguang
 * @Date 2022/10/20/12:01
 */
public class SyncronizedProOne {

    private final static Logger logger = LoggerFactory.getLogger(ObjectAgeTest.class);

    private static int money = 0;

    private static CountDownLatch countDownLatch = new CountDownLatch(2);

    public static void increment(int count){
        money+=count;
    }

    public static void main(String[] args) throws InterruptedException {

        logger.info("当前的money的值是：{}",money);

        new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                increment(2);
            }
            countDownLatch.countDown();
        },"t1").start();


        new Thread(()->{
            for (int i = 0; i < 40000; i++) {
                increment(5);
            }
            countDownLatch.countDown();
        },"t2").start();

        countDownLatch.await();

        logger.info("当前的money的值是：{}",money);
    }

}
